

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace RWG.Sharepoint.Webparts.SLDiscussion
{
    [Guid("51826736-cf6f-4bb8-bedb-fdf5f6ff1faa")]
    public class SLDiscussion : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string discussionListName = null;
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Discussion List Name")]
        [WebDescription("The Name of the Sharepoint Disscussion List")]
        public string DiscussionListName
        {
            get
            {
                if (discussionListName == null)
                {
                    discussionListName = "Team Discussion";
                }
                return discussionListName;
            }
            set { discussionListName = value; }
        }
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DefaultValue("slhost")]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Silverlight Control Name")]
        [WebDescription("The Name of the Silverlight control")]
        public string SLControlName
        { get; set; }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DefaultValue(600)]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Tree Height")]
        [WebDescription("The Height of the treeview section")]
        public string ThreadHeight
        {get;set;}

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DefaultValue(600)]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Tree Width")]
        [WebDescription("The Width of the treeview section")]
        public string ThreadWidth
        { get; set; }

   

      

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DefaultValue("msgdiv")]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Message Control Name")]
        [WebDescription("The Name of the MessageControl")]
        public string MessageControlName
        { get; set; }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DefaultValue(600)]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Message Height")]
        [WebDescription("The Height of the Message section")]
        public string MessageHeight
        { get; set; }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [DefaultValue(600)]
        [System.ComponentModel.Category("Discussion Preoperties")]
        [WebDisplayName("Message Width")]
        [WebDescription("The Width of the Message section")]
        public string MessageWidth
        { get; set; }
        public SLDiscussion()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
               {
                    base.CreateChildControls();
                    string LIstID = GetLIstIDForName(DiscussionListName).ToString();
                    string slStringFormat = @"<table width='600'><tr width='100%'><td width='50%'>"
                        + "<div id=\"{0}\" style=\"width:auto; height:auto\"></div>" +
                        "<script type=\"text/javascript\">"
                       + "createSL('{0}', null, null, '/_layouts/SLDiscussion/RWG.Silverlight.SPDiscussion.xap', 'listid={1},messagecontrolname={2}');</script></td><td  width='50%' valign='top'><div id=\"{2}\" style=\"width:auto; height:auto;\"></div></td></tr></table>";
                    
                    string slString = String.Format(slStringFormat,SLControlName,LIstID.ToString(),MessageControlName);
                    LiteralControl silverlightHost = new LiteralControl(slString);

                    this.Controls.Add(silverlightHost);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        
        string GetLIstIDForName(string DiscussionListName)
        {
          SPListCollection lists= SPContext.Current.Web.Lists;
            foreach (SPList list in lists)
            {
                if (list.Title==DiscussionListName)
                    return list.ID.ToString();
            }
            return null;
        }
        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ClientScriptManager cs = Page.ClientScript;// Include the required javascript file.
            if (!cs.IsClientScriptIncludeRegistered("sl_javascript"))
                cs.RegisterClientScriptInclude(this.GetType(), "sl_javascript", "/_LAYOUTS/SL3/Silverlight.js");
            if (!cs.IsClientScriptIncludeRegistered("spsl_javascript"))
                cs.RegisterClientScriptInclude(this.GetType(), "spsl_javascript", "/_LAYOUTS/SL3/SpSilverlight.js");
        }
        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
